<?php

namespace App\Http\Controllers\Home;

use App\Http\Requests\Login;
use App\Models\Configs;
use App\Models\IntegralLogs;
use App\Models\TemporaryStorages;
use App\Models\UserInfos;
use App\Models\Users;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class RegisterController extends Controller
{

    /**
     * 注册页面
     * @param Request $request
     * @return bool
     */
    public function register(Request $request)
    {
        return view('home.register.register');
    }

    /**
     * 供应商注册第一步
     * @param Request $request
     * @param mobile 手机号
     * @param password 密码
     * @param confirmc_pass 确认密码
     * @param certification 认证资料(数组)
     * @return bool
     */
    public function supplierStep1(Login $request){
        $data = [
            'mobile' => (string)$request->post('mobile'),
            'password' => (string)$request->post('password'),
            'confirmc_pass' => (string)$request->post('confirmc_pass'),
            'certification' => (array)$request->post('certification'),
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        if(!preg_match('/^((13|14|15|16|17|18)[0-9]\d{8})$/', $data['mobile']))   return $this->writeJson(1,null,'请填写正确的手机号码');
        if($data['password'] !== $data['confirmc_pass']){
            return $this->writeJson(1,null,'两次密码输入不一致,请重新输入');
        }
        //检测手机号是否被注册
        $check_member_mobile = Users::getUserInfo(['mobile' => $data['mobile']]);
        if (!empty($check_member_mobile)) {
            return $this->writeJson(1,null,'该手机号已经注册');
        }
        unset($data['confirmc_pass']);
        $id = TemporaryStorages::create($data);
        return $this->writeJson(0,$id['id']);
    }

    /**
     * 供应商注册
     * @param Request $request
     * @param tid 暂存表id
     * @param company_name 公司名称
     * @param company_email 公司邮箱
     * @param company_phone 公司电话
     * @param master_name 负责人姓名
     * @param master_phone 负责人电话
     * @param master_position 负责人职位
     * @param company_type 公司类型
     * @param company_industry 公司行业
     * @param company_size 公司规模
     * @return bool
     */
    public function supplierStep2(Request $request)
    {
        try {
            $tid = (int)$request->get('tid');//暂存表id
            $info = TemporaryStorages::where('id',$tid)->first();//暂存信息
            if (empty($info)) return $this->writeJson(1, null, '参数错误');

            $integral = Configs::getConfig('integral');//注册送积分
            $base = Configs::getConfig('base');//获取头像
            $now = date('Y-m-d H:i:s');
            $data = [
                'avatar' => $base['default_avatar'],
                'user_sn' => 'HR'.substr(time(),-6),
                'username' => $info['mobile'],
                'mobile' => $info['mobile'],
                'password' => bcrypt($info['password']),
                'integral' => $integral['bus_register'],
                'register_ip' => $request->ip(),
                'login_at' => $now,
                'login_ip' => $request->ip(),
                'type' => 2,
                'status' => 2,
                'created_at' => $now,
                'updated_at' => $now,
            ];
            $user_id = Users::insertGetId($data);
            //信息存入扩展表
            $data_info = [
                'user_id' => $user_id,
                'company_name' => (string)$request->post('company_name',''),
                'company_email' => (string)$request->post('company_email',''),
                'company_phone' => (string)$request->post('company_phone',''),
                'master_name' => (string)$request->post('master_name',''),
                'master_phone' => (string)$request->post('master_phone',''),
                'master_position' => (string)$request->post('master_position',''),
                'company_type' => (int)$request->post('company_type',''),
                'company_industry' => (int)$request->post('company_industry',''),
                'company_size' => (int)$request->post('company_size',''),
                'certification' => $info['certification'],
            ];
            UserInfos::create($data_info);
            //添加日志记录
            $integral = [
                'user_id' => $user_id,
                'user_name' => $info['mobile'],
                'integral' => '+'.$integral['hr_register'],
                'desc' => '注册送积分',
                'type' => 1,
            ];
            IntegralLogs::create($integral);
            return $this->writeJson(0,route('home.login.login'), '新增用户信息成功');
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }

    /**
     * HR注册注册第一步
     * @param Request $request
     * @param mobile 手机号
     * @param password 密码
     * @param confirmc_pass 确认密码
     * @return bool
     */
    public function hrStep1(Login $request){
        $data = [
            'mobile' => (string)$request->post('mobile'),
            'password' => (string)$request->post('password'),
            'confirmc_pass' => (string)$request->post('confirmc_pass'),
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
        if(!preg_match('/^((13|14|15|16|17|18)[0-9]\d{8})$/', $data['mobile']))   return $this->writeJson(1,'请填写正确的手机号码');
        if($data['password'] !== $data['confirmc_pass']){
            return $this->writeJson(1,null,'两次密码输入不一致,请重新输入');
        }
        //检测手机号是否被注册
        $check_member_mobile = Users::getUserInfo(['mobile' => $data['mobile']]);
        if (!empty($check_member_mobile)) {
            return $this->writeJson(1,null,'该手机号已经注册');
        }
        unset($data['confirmc_pass']);
        $id = TemporaryStorages::insertGetId($data);
        return $this->writeJson(0,$id);
    }

    /**
     * HR注册第二步
     * @param Request $request
     * @param tid 暂存表id
     * @param username 用户名
     * @param email 邮箱
     * @param sex 姓名：1男 2女
     * @param company_name 公司名称
     * @param hr_department 部门
     * @param hr_position 职位
     * @param company_type 公司类型
     * @param company_industry 公司行业
     * @param company_size 公司规模
     * @param hr_size HR部门人数
     * @return bool
     */
    public function hrStep2(Request $request)
    {
        try {
            $tid = (int)$request->get('tid');//暂存表id
            $info = TemporaryStorages::where('id',$tid)->first();//暂存信息
            if (empty($info)) return $this->writeJson(1, null, '参数错误');

            $integral = Configs::getConfig('integral');//注册送积分
            $base = Configs::getConfig('base');//获取头像
            $now = date('Y-m-d H:i:s');
            $data = [
                'avatar' => $base['default_avatar'],
                'user_sn' => 'HR'.substr(time(),-6),
                'username' => $info['mobile'],
                'nickname' => (string)$request->post('username',''),
                'mobile' => $info['mobile'],
                'password' => bcrypt($info['password']),
                'email' => (string)$request->post('email',1),
                'sex' => (int)$request->post('sex',1),
                'register_ip' => $request->ip(),
                'integral' => $integral['hr_register'],
                'login_at' => $now,
                'login_ip' => $request->ip(),
                'type' => 1,
                'status' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ];
            $user_id = Users::insertGetId($data);
            //信息存入扩展表
            $data_info = [
                'user_id' => $user_id,
                'company_name' => (string)$request->post('company_name',''),
                'hr_department' => (string)$request->post('hr_department',''),
                'hr_position' => (string)$request->post('hr_position',''),
                'company_type' => (int)$request->post('company_type',0),
                'company_industry' => (int)$request->post('company_industry',0),
                'company_size' => (int)$request->post('company_size',0),
                'hr_size' => (int)$request->post('hr_size',0),
            ];
            UserInfos::insert($data_info);
            //添加日志记录
            $integral = [
                'user_id' => $user_id,
                'user_name' => $info['mobile'],
                'integral' => '+'.$integral['bus_register'],
                'desc' => '注册送积分',
                'type' => 1,
            ];
            IntegralLogs::create($integral);
            return $this->writeJson(0,route('home.login.login'), '新增用户信息成功');
        } catch (\Exception $e) {
            return $this->writeJson(1, null, $e->getMessage());
        }
    }


}